<?php

declare(strict_types=1);

namespace app\index\controller;

use app\common\model\UserPacket;
use app\common\model\Packet as PacketModel;
use app\common\model\ProductCate;
use app\common\model\ProductSpec;
use app\common\tool\Result;
use think\facade\Cache;
use think\facade\View;
use think\Response;

class Packet extends Base
{

    /**
     * 抢红包
     */
    public function index()
    {
        $keyword = $this->request->get('keyword');
        View::assign([
            'keyword' => $keyword
        ]);
        return View::fetch();
    }
    /**
     * 抢红包详情
     */
    public function details($id)
    {
        $info = PacketModel::where(['id' => $id])->append(['sy', 'sytoal'])->find();
        if (empty($info)) return redirect('/');
        View::assign([
            'info'  => $info
        ]);
        return View::fetch();
    }
    /**
     * 获取抢红包列表
     */
    public function getList(): Response
    {
        $param = $this->request->get();
        $map[] = ['end_stock', '>', 0];
        $like = [];
        if (isset($param['keyword']) && $param['keyword']) {
            $like['field'] = 'title';
            $like['keyword'] = $param['keyword'];
        }
        $list = PacketModel::getPageList($map, '*', $param['page'], 20, false, '', '', ['key' => 'id', 'value' => 'asc'], $like);
        foreach ($list['data'] as &$v) {
            $v->sytitle = ($v->yield * 100);
            $v->sytoaltitle = round(($v->price + $v->yield * $v->price * $v->gold), 0) . '金币+' . round(($v->yield * $v->price * $v->integral), 0) . '积分';
        }
        return Result::fastReturn($list);
    }
    /**
     * 释放记录
     */
    public function list()
    {

        return View::fetch();
    }
    /**
     * 获取释放记录
     */
    public function mygGetList(): Response
    {
        $param = $this->request->get();
        $map[] = ['uid', '=', $this->user->id];
        $map[] = ['pay_status', '=', 1];
        $list = UserPacket::getPageList($map, '*', 1, 20, false);
        foreach ($list['data'] as &$v) {
            $v->sytitle = ($v->yield * 100);
            $v->status_name = $v->status == 1 ? "未释放":"已释放";
        }
        return Result::fastReturn($list);
    }

    /**
     * 创建抢红包
     */
    public function submit(): Response
    {
        return Result::fastReturn(UserPacket::createOrder($this->request->post(), $this->user));
    }
    /**
     * 获取抢红包支付信息
     */
    public function payment($id)
    {
        $info = UserPacket::getPayment($id);
        if (empty($info['data'])) return redirect(url('user/index')->build());
        View::assign('info', $info['data']['order']);
        View::assign('pay_list', $info['data']['method']);
        return View::fetch();
    }
    /**
     * 支付抢红包
     */
    public function confirmPayment(): Response
    {
        try {
            return Result::fastReturn(UserPacket::payment($this->request->post(), $this->user));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
